import wx
import widget

class Miyuki(widget.Interactor):
    """Miyuki interactor"""
    
    def bind(self):
        super(Miyuki, self).bind()
        
        view = self.view
        menu = view.MenuBar
        tool = view.ToolBar
        
        view.Bind(wx.EVT_MENU, self.orientSplit, menu.file.orientSplit)
        view.Bind(wx.EVT_MENU, self.toggleSplit, menu.file.toggleSplit)
        view.Bind(wx.EVT_MENU, self.close, menu.file.close)
        
        view.Bind(wx.EVT_MENU, self.save, menu.image.post.save)
        view.Bind(wx.EVT_MENU, self.delete, menu.image.post.delete)
        view.Bind(wx.EVT_MENU, self.queue, menu.image.post.queue)
        view.Bind(wx.EVT_MENU, self.download, menu.image.post.download)
        view.Bind(wx.EVT_MENU, self.export, menu.image.post.export)
##        view.Bind(wx.EVT_MENU, self.invite, menu.image.post.invite)
        
        view.Bind(wx.EVT_MENU, self.showAbout, menu.help.about)
        
        view.Bind(wx.EVT_MENU, self.save, tool.save.tool)
        view.Bind(wx.EVT_MENU, self.delete, tool.delete.tool)
        view.Bind(wx.EVT_MENU, self.queue, tool.queue.tool)
        view.Bind(wx.EVT_MENU, self.download, tool.download.tool)
        view.Bind(wx.EVT_MENU, self.export, tool.export.tool)
##        view.Bind(wx.EVT_MENU, self.invite, tool.invite.tool)
        view.Bind(wx.EVT_MENU, self.orientSplit, tool.orientSplit.tool)
        view.Bind(wx.EVT_MENU, self.toggleSplit, tool.toggleSplit.tool)
        view.Bind(wx.EVT_MENU, self.showAbout, tool.about.tool)
        view.Bind(wx.EVT_MENU, self.close, tool.close.tool)
        
        view.Bind(wx.EVT_COMMAND_RIGHT_CLICK, self.popupPostsMenu, view.posts)
        
        view.Bind(wx.EVT_LIST_ITEM_SELECTED, self.show, view.posts)
        view.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.unshow, view.posts)
        
        view.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, self.filter, tool.filter)
        view.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN, self.unfilter, tool.filter)
        view.Bind(wx.EVT_TEXT_ENTER, self.filter, tool.filter)
        
        view.Bind(wx.EVT_MENU, self.save, view.posts.menu.save)
        view.Bind(wx.EVT_MENU, self.delete, view.posts.menu.delete)
        view.Bind(wx.EVT_MENU, self.queue, view.posts.menu.queue)
        view.Bind(wx.EVT_MENU, self.download, view.posts.menu.download)
        view.Bind(wx.EVT_MENU, self.export, view.posts.menu.export)
##        view.Bind(wx.EVT_MENU, self.invite, view.posts.menu.invite)
        
        findId = wx.NewId()
        view.Bind(wx.EVT_MENU, self.find, id=findId)
        saveId = wx.NewId()
        view.Bind(wx.EVT_MENU, self.save, id=saveId)
        deleteId = wx.NewId()
        view.Bind(wx.EVT_MENU, self.delete, id=deleteId)
        
        table = [(wx.ACCEL_CTRL, ord('F'), findId),
                 (wx.ACCEL_CTRL, ord('S'), saveId),
                 (wx.ACCEL_NORMAL , wx.WXK_DELETE, deleteId),]
        view.SetAcceleratorTable(wx.AcceleratorTable(table))
        
    def showAbout(self, event):
        event.Skip()
        self.presenter.showAbout()
    
    def orientSplit(self, event):
        event.Skip()
        self.view.orientSplit()

    def toggleSplit(self, event):
        event.Skip()
        self.presenter.toggleSplit()
    
    def filter(self, event):
        event.Skip()
        if not event.String: return
        
        self.presenter.filter(event.String)
        self.view.posts.SetFocus()
        if self.view.posts.GetItemCount():
            state = wx.LIST_STATE_FOCUSED | wx.LIST_STATE_SELECTED
            self.view.posts.SetItemState(0, state, state)         
    
    def unfilter(self, event):
        event.Skip()
        self.presenter.unfilter()
    
    def show(self, event):
        event.Skip()
        self.presenter.show()
    
    def find(self, event):
        event.Skip()
        self.view.ToolBar.filter.SetFocus()
        
    def delete(self, event):
        event.Skip()
        self.presenter.delete()
    
    def popupPostsMenu(self, event):
        event.Skip()
        posts = self.view.posts
        posts.PopupMenu(posts.menu)
    
    def unshow(self, event):
        event.Skip()
        self.presenter.enablePostsMenu()
        
    def queue(self, event):
        event.Skip()
        self.presenter.queue()
        
    def download(self, event):
        event.Skip()
        self.presenter.download()
        
    def save(self, event):
        event.Skip()
        self.presenter.save()
        
    def export(self, event):
        event.Skip()
        self.presenter.export()
        
    def invite(self, event):
        event.Skip()
        self.presenter.invite()
